import axios from 'axios'
import { msgWarning,msgError } from 'lib@/utils/el-utils'
//import { clearUserInfo, getUserInfo } from 'lib@/utils/user'
// 创建axios实例
const service = axios.create({
  timeout: 15000, // 请求超时时间
  withCredentials: true,
  headers: {
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache'
  }
})
// request拦截器
service.interceptors.request.use(config => {
  let userInfo = null
  config.headers.token = userInfo ? userInfo.token : ''
  return config
}, error => {
  console.log(error)
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    const response = error.response
    if (response) {
      switch (response.status) {
        case 401 :
        case 403 :
          msgWarning('请先登录')
          //clearUserInfo()
          window.location.href = '/login.html'
          break
        case 404:
          msgError('服务器异常')
          break
        case 500:
          msgError('系统异常')
          break
      }
    }
    console.log('异常:', error)
    return Promise.reject(error)
  }
)

export default service